package algorithm.leetcode.I401to600;


//         a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z
//       |   |   |   |   |   | 2 | + | 2 |   |   |   |   |   |   |   |   |   |   | 2 |   | + |   | + | + |   | +
// one   |   |   |   |   | 1 |   |   |   |   |   |   |   |   | 1 | 1 |   |   |   |   |   |   |   |   |   |   |
// two   |   |   |   |   |   |   |   |   |   |   |   |   |   |   | 1 |   |   |   |   | 1 |   |   | 1 |   |   |
// three |   |   |   |   | 2 |   |   | 1 |   |   |   |   |   |   |   |   |   | 1 |   | 1 |   |   |   |   |   |
// four  |   |   |   |   |   | 1 |   |   |   |   |   |   |   |   | 1 |   |   | 1 |   |   | 1 |   |   |   |   |
// five  |   |   |   |   | 1 | 1 |   |   | 1 |   |   |   |   |   |   |   |   |   |   |   |   | 1 |   |   |   |
// six   |   |   |   |   |   |   |   |   | 1 |   |   |   |   |   |   |   |   |   | 1 |   |   |   |   | 1 |   |
// seven |   |   |   |   | 2 |   |   |   |   |   |   |   |   | 1 |   |   |   |   | 1 |   |   | 1 |   |   |   |
// eight |   |   |   |   | 1 |   | 1 | 1 | 1 |   |   |   |   |   |   |   |   |   |   | 1 |   |   |   |   |   |
// nine  |   |   |   |   | 1 |   |   |   | 1 |   |   |   |   | 2 |   |   |   |   |   |   |   |   |   |   |   |
// zero  |   |   |   |   | 1 |   |   |   |   |   |   |   |   |   | 1 |   |   | 1 |   |   |   |   |   |   |   | 1

/**
 * 没什么意思
 *
 * “z” 只在 “zero” 中出现。
 * “w” 只在 “two” 中出现。
 * “u” 只在 “four” 中出现。
 * “x” 只在 “six” 中出现。
 * “g” 只在 “eight” 中出现。
 *
 * 区分3 5 7
 * “h” 只在 “three” 和 “eight” 中出现。
 * “f” 只在 “five” 和 “four” 中出现。
 * “s” 只在 “seven” 和 “six” 中出现。
 *
 * 区分1 9
 * “i” 在 “nine”，“five”，“six” 和 “eight” 中出现。
 * “n” 在 “one”，“seven” 和 “nine” 中出现
 */

public class Q423 {

    public String originalDigits(String s) {
        // building hashmap letter -> its frequency
        char[] count = new char[26 + (int)'a'];
        for(char letter: s.toCharArray()) {
            count[letter]++;
        }

        // building hashmap digit -> its frequency
        int[] out = new int[10];
        // letter "z" is present only in "zero"
        out[0] = count['z'];
        // letter "w" is present only in "two"
        out[2] = count['w'];
        // letter "u" is present only in "four"
        out[4] = count['u'];
        // letter "x" is present only in "six"
        out[6] = count['x'];
        // letter "g" is present only in "eight"
        out[8] = count['g'];
        // letter "h" is present only in "three" and "eight"
        out[3] = count['h'] - out[8];
        // letter "f" is present only in "five" and "four"
        out[5] = count['f'] - out[4];
        // letter "s" is present only in "seven" and "six"
        out[7] = count['s'] - out[6];
        // letter "i" is present in "nine", "five", "six", and "eight"
        out[9] = count['i'] - out[5] - out[6] - out[8];
        // letter "n" is present in "one", "nine", and "seven"
        out[1] = count['n'] - out[7] - 2 * out[9];

        // building output string
        StringBuilder output = new StringBuilder();
        for(int i = 0; i < 10; i++)
            for (int j = 0; j < out[i]; j++)
                output.append(i);
        return output.toString();
    }

    public static void main(String[] args) {
        Q423 q423 = new Q423();
        System.out.println(q423.originalDigits("owoztneoer"));
    }
}
